package guicomponents;

import messages.HitMessage;
import messages.Message;
import connection.Connection;
import gamecomponents.BigShip;
import gamecomponents.MediumShip;
import gamecomponents.MotherShip;
import gamecomponents.Position;
import gamecomponents.SmallShip;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class BattleSquare extends JPanel {

    private Position position;
    private Connection con;
    private static Message hitMess;
    private boolean ACTIVE = true,
            VERTICAL = true,
            SHIP = false,
            HITTED = false;
    public static List mainList = new LinkedList<BattleSquare>();
    public List positionList = new ArrayList<Position>();
    public boolean FIRE = false;
    public static int x = 0, y = 0, bNum = 0, mNum = 0, meNum = 0, sNum = 0;
    public static Color Def_BackgroundColor = Color.LIGHT_GRAY;
    public static Color OnFocus_BackgroundColor = Color.DARK_GRAY;
    public static Color Ship_BackgroundColor = Color.GREEN;
    public static MotherShip motherShip;
    public static BigShip bigShip;
    public static MediumShip mediumShip;
    public static SmallShip smallShip;

    BattleSquare(boolean active) {
        this.ACTIVE = active;
        this.position = new Position(x, y);
        x++;
        if (x == 8) {
            x = 0;
            y++;
        }
        this.addMouseListener(listener);
        this.setBackground(Def_BackgroundColor);
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        updateList();
    }
    //addolja a mainListbe az aktuális BS-t

    private void updateList() {
        mainList.add(this);
    }

    //visszaadja a BS pozicióját
    public Position getPos() {
        return position;
    }
    //beállítja, hogy ez a BS meglett lőve(enemyFieldhez kell)

    public void setHit() {
        this.HITTED = true;
    }
    //megmondja, hogy ez a BS meglett e lőve

    public boolean hitted() {
        if (this.HITTED) {
            return true;
        } else {
            return false;
        }
    }
    //van e ezen a BS-en hajó

    public boolean getShip() {
        return this.SHIP;
    }
    //megmondja, hogy van e még élő hajónk

    public boolean shipReturn() {
        if (motherShip.getLife() == 0 && mediumShip.getLife() == 0 && bigShip.getLife() == 0
                && smallShip.getLife() == 0) {
            return false;
        }
        return true;
    }
    //megvizsgálja, hogy levan e rakva minden hajó

    public static boolean testShips() {
        if (bNum == 1 && mNum == 1 && meNum == 1 && sNum == 1) {
            return true;
        } else {
            return false;
        }
    }
    //beállítja, hogy van ezen a BS-en hajó

    public void setOnShip(boolean SHIP) {
        this.SHIP = SHIP;
    }

    public Message sendHit(boolean HIT, boolean LIFE) {
        hitMess = new HitMessage(this.getPos(), HIT, LIFE);
        return hitMess;
    }
    
    //visszaadja a BS-ek listáját
    public static List getTheListOfTheSquares() {
        return mainList;
    }
    MouseListener listener = new MouseListener() {

        public void mousePressed(MouseEvent e) {
            if (BattleSquare.this.ACTIVE) {
                BattleSquare.this.setBackground(Color.RED);
                System.out.println(BattleSquare.this.getPos());
                BattleSquare.this.setHit();
                //sendMessagek
                con = Connection.getInstance();

            } else {
                if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
                    VERTICAL = !VERTICAL;
                }
                if (Gui.getSelectedShip() == 4 && !BattleSquare.this.getShip() && mNum == 0) {
                    motherShip = MotherShip.getInstance();
                    if (motherShip.locAvailable(mainList, BattleSquare.this, VERTICAL)) {
                        motherShip.setLoc(mainList, BattleSquare.this, VERTICAL);
                        mNum++;
                    }
                }
                if (Gui.getSelectedShip() == 3 && !BattleSquare.this.getShip() && bNum == 0) {
                    bigShip = BigShip.getInstance();
                    if (bigShip.locAvailable(mainList, BattleSquare.this, VERTICAL)) {
                        bigShip.setLoc(mainList, BattleSquare.this, VERTICAL);
                        bNum++;
                    }
                }
                if (Gui.getSelectedShip() == 2 && !BattleSquare.this.getShip() && meNum == 0) {
                    mediumShip = MediumShip.getInstance();
                    if (mediumShip.locAvailable(mainList, BattleSquare.this, VERTICAL)) {
                        mediumShip.setLoc(mainList, BattleSquare.this, VERTICAL);
                        meNum++;
                    }
                }
                if (Gui.getSelectedShip() == 1 && !BattleSquare.this.getShip() && sNum == 0) {
                    smallShip = SmallShip.getInstance();
                    if (smallShip.locAvailable(mainList, BattleSquare.this, VERTICAL)) {
                        smallShip.setLoc(mainList, BattleSquare.this, VERTICAL);
                        sNum++;
                    }
                }
            }

        }

        public void mouseClicked(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
            if (!BattleSquare.this.getShip() && !hitted()) {
                BattleSquare.this.setBackground(OnFocus_BackgroundColor);
            }
        }

        public void mouseExited(MouseEvent e) {
            if (!BattleSquare.this.getShip() && !hitted()) {
                BattleSquare.this.setBackground(Def_BackgroundColor);
            }
        }
    };
}